package edu.pku.sei.mte.mtemodel.mtmodel.rulebased;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.pku.sei.mte.mtemodel.metamodel.RTypeClass;
import edu.pku.sei.mte.mtemodel.mtmodel.Transformation;
import edu.pku.sei.mte.mtemodel.mtmodel.common.Key;
import edu.pku.sei.mte.utilities.Visitor;

public class RBTransformation extends Transformation {
	private List<Rule> rules;
	private Map<RTypeClass,Key> objectKeys;
	
	public List<Rule> getRules() {
		if(rules==null){
			rules = new ArrayList<Rule>();
		}
		return rules;
	}
	public void addRule(Rule rule){
		rule.setHost(this);
		getRules().add(rule);
	}
	public Map<RTypeClass, Key> getObjectKeys() {
		if(objectKeys==null){
			objectKeys = new HashMap<RTypeClass,Key>();
		}
		return objectKeys;
	}
	
	public void addObjectKey(RTypeClass type,Key key){
		this.getObjectKeys().put(type, key);
	}
	
	
	public Rule getRule(String name){
		for(Rule r : getRules()){
			if(r.getName().equals(name))
				return r;
		}
		return null;
	}
	
	public List<Rule> getTopLevelRules(){
		List<Rule> top = new ArrayList<Rule>();
		for(Rule r : rules){
			if(r.getPriority()==0)
				top.add(r);
		}
		
		return top;
	}

	@Override
	public Object accept(Visitor visitor) throws Exception {
		return visitor.visitRBTransformation(this);
	}
}
